<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\index\model\tan\Bankcard as b;
use app\index\controller\tan\ReturnData as js;
use app\index\controller\Sms\AliYunSms;
use app\index\model\tan\PhoneCode;
use think\Session;
use think\Db;
/**
 * 银行卡
 */
class Bankcard extends Controller
{

    /**
     * @var b
     */
    private static $Bankcard;
    public function __construct(b $Bankcard) {
        self::$Bankcard=$Bankcard;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()    {
        //
        
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()    {
        //
    }

    /**
     * 添加银行卡
     * @param Request $request
     * @param PhoneCode $PhoneCode
     */
    public function save(Request $request,PhoneCode $PhoneCode)
    {

        $post =$request->post();
        tan\Ret::IfNoTrue( $this->validate($post, [
            "bankcard_id|银行卡"=>"require|alphaNum|max:20",
            "opening_bank|开会行"=>"require",
            //"opening_bank_address|开会行地址"=>"require",
            "code|验证码"=>"require|number",
            "phone|手机电话"=>"require|^1[0-9]{10}$",
            "name|开户人名称"=>"require|max:10",
        ]), 10006);
        $d=$PhoneCode->query([
            "phone"=>$post['phone'],
            "code"=>$post['code'],
            "user_id"=>Session::get("user.id")
        ]);
        tan\Ret::IfEmpty($d, 10004, "验证码错误");
        $bool=self::$Bankcard->save_($post);
        if($bool){
            js::ok();
        } else {
            js::err(10002,"未知");
        }
        
    }

    /**
     * 显示指定的银行卡
     *
     * @param  int  $id
     */
    public function read($id)
    {
        tan\Ret::IfEmptyExit(self::$Bankcard->query_($id) , 10003, "无数据或该银行卡无权限查看");
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 更新银行卡
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $post=$request->post();
        $rule=[
            "bankcard_id|银行卡"=>"require|alphaNum|max:20",
            "opening_bank|开会行"=>"require",
            "opening_bank_address|开会行地址"=>"max:10",
            "name|名称"=>"max:10",
            "phone|手机电话"=>"require|^1[0-9]{10}$",
            "default|是否设为默认银行卡"=>"in:0,1",
            "state|是否禁用"=>"in:0,1"
        ];
        tan\Ret::IfNoTrue( $this->validate($post, $rule), 10009);
        $data=tan\Eliminate::E($post, $rule);
        tan\Ret::Exit_(self::$Bankcard->up($id,$data), 10009,"无任何修改或无权修改");
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
    
    /**
     * 银行卡短信
     * @param \app\index\controller\AliYunSms $sms
     * @param \app\index\controller\PhoneCode $pc
     * @param type $mobile
     * @return json
     */
    public function sms(AliYunSms $sms, PhoneCode $pc,$mobile){
        $rund= rand(1000, 9999);
        $data= $sms ->code_type($rund,"reg")->setRecNum($mobile);
        if($data['code'] == 666){
            $pc->msg="reg";
            $pc->user_id =Session::get("user.id");
            $pc->createtime =date("Y-m-d H:i:s");
            $pc->code=$rund;
            $pc->phone=$mobile;
            $pc->save();
        }
       return $data;
    }
}
